﻿Imports Data

Public Class FormaPagos


    Private Property id_formaPago As Integer

    Private Sub FormaPagos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargarLV()
    End Sub

    Private Function CargarLV() As Boolean
        Try
            Me.lvFormaPago.Items.Clear()
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of Data.Forma_Pago) = db.Forma_Pago.Select(Function(p) p).ToList
                For Each item In lista
                    Me.lvFormaPago.Items.Add(New ListViewItem(New String() {item.id, item.nombre}) With {.Name = item.id})
                Next
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If (Me.btnIngresar.Text = "Ingresar") Then
                If Me.Ingresar() Then
                    MsgBox("Ingresado Existosamente", MsgBoxStyle.OkOnly)
                    Me.CargarLV()
                    Me.LimpiarInterfaz()
                End If
            Else
                Me.id_formaPago = Me.lvFormaPago.SelectedItems(0).Name
                If Me.Modificar() Then
                    MsgBox("Modificado Existosamente", MsgBoxStyle.OkOnly)
                    Me.CargarLV()
                    Me.LimpiarInterfaz()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function LimpiarInterfaz() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.txtNombre.Focus()
            Me.btnIngresar.Text = "Ingresar"
        Catch ex As Exception

        End Try
    End Function

    Private Function Ingresar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim n_formaPago As New Data.Forma_Pago
                n_formaPago.nombre = txtNombre.Text
                db.Forma_Pago.InsertOnSubmit(n_formaPago)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function Modificar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim formaPago As Data.Forma_Pago = db.Forma_Pago.Where(Function(p) p.id = Me.id_formaPago).Select(Function(p) p).FirstOrDefault
                formaPago.nombre = Me.txtNombre.Text
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Function Eliminar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.id_formaPago = Me.lvFormaPago.SelectedItems(0).Name
                Dim forma_pago As Data.Forma_Pago = db.Forma_Pago.Where(Function(p) p.id = id_formaPago).Select(Function(p) p).FirstOrDefault
                db.Forma_Pago.DeleteOnSubmit(forma_pago)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub ModificarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Try
            Me.ModificarInterfaz()
        Catch ex As Exception

        End Try
    End Sub

    Private Function ModificarInterfaz() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.id_formaPago = Me.lvFormaPago.SelectedItems(0).Name
                Dim forma_pago As Data.Forma_Pago = db.Forma_Pago.Where(Function(p) p.id = id_formaPago).Select(Function(p) p).FirstOrDefault
                Me.txtNombre.Text = forma_pago.nombre
                Me.btnIngresar.Text = "Modificar"
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Me.LimpiarInterfaz()
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        If Me.Eliminar() Then
            MsgBox("Eliminado Exitosamente")
            Me.LimpiarInterfaz()
            Me.CargarLV()
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub
End Class